/*

	This file loads data from the Firm Statistics Register (abbreviated FIRM by DST)

*/

cd ${work}1_data

local start	2001
local end	2015
local list	"jurnr gf_aarsv gf_oms gf_aare gf_ainv gf_egul gf_at gf_gr019_db07 gf_gr036_db07 gf_nace_db07 gf_gr127_db07 version"

*LOAD
foreach y of numlist `start'/`end' {
	
	use input\firm`y' if jurnr!="" , clear
	
	keep `list'	
	gen year=`y'
		
	qui sum version
	keep if version==r(max)
	drop version

	save temp\firm_`y', replace	

}

clear
local command use
foreach y of numlist `start'/`end' {
	`command' temp\firm_`y'
	erase temp\firm_`y'.dta
	local command "append using"
}

*MODIFY
rename gf_aarsv		employees
rename gf_oms		sales
rename gf_aare		profits
rename gf_ainv		investments
rename gf_egul		equity
rename gf_at		assets
rename gf_gr019_db07 nace1
rename gf_nace_db07	nace4
rename gf_gr036_db07 g36
rename gf_gr127_db07 g127

*convert from dkk to 1000 dkk
foreach v of varlist sales equity profits assets investments {
    replace `v'=`v'/1000
}

*SAVE
order jurnr year nace1 nace4
sort jurnr year
compress
save output\firm_it, replace